package conf

import (
	"io/ioutil"
	"log"

	"gopkg.in/yaml.v2"
)

type Config struct {
	URL         string `json:"url"`
	User        string `json:"user"`
	Password    string `json:"password"`
	MinSeverity int    `json:"minseverity"`
}

func LoadFile(filename string) *Config {
	content, err := ioutil.ReadFile(filename)
	if err != nil {
		log.Fatal(err)
	}
	cfg, err := Load(string(content))
	if err != nil {
		log.Fatal(err)
	}

	return cfg
}

func Load(s string) (*Config, error) {
	cfg := &Config{}
	err := yaml.Unmarshal([]byte(s), cfg)
	if err != nil {
		return nil, err
	}

	return cfg, nil
}
